﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Data.SqlClient;

namespace Bll
{
    class CInCode
    {
        /// <summary>
        /// 纯粹是创建一个新的对象
        /// </summary>
        /// <returns></returns>
        public static CInCode CreateBasicUser()
        {
            return new CInCode();
        }
        /// <summary>
        /// string 到 string 的解码
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string decode(string value)
        {
            byte[] buffer = Convert.FromBase64String(value);
            var memoryStream = new MemoryStream(buffer);
            DES crypt = DES.Create();
            ICryptoTransform transform = crypt.CreateEncryptor();
            var cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Read);

            var streamReader = new StreamReader(cryptoStream);

            string back = streamReader.ReadToEnd();

            streamReader.Close();
            cryptoStream.Close();
            memoryStream.Close();

            return back;
        }
        /// <summary>
        /// string到string的加密
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string incode(string value)
        {
            var memoryStream = new MemoryStream();
            DES crypt = DES.Create();
            ICryptoTransform transform = crypt.CreateEncryptor();
            var cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);

            var streamWriter = new StreamWriter(cryptoStream);

            streamWriter.Write(value);
            streamWriter.Flush();
            cryptoStream.FlushFinalBlock();
            memoryStream.Flush();

            string back = Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);

            streamWriter.Close();
            cryptoStream.Close();
            memoryStream.Close();

            return back;
        }

        #region   加密解密

        private static string key = "加密钥匙c";
        public static string Encrypt(string encryptString)
        {
            byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 7));
            byte[] keyIV = keyBytes;
            byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
            var provider = new DESCryptoServiceProvider();
            var mStream = new MemoryStream();
            var cStream = new CryptoStream(mStream, provider.CreateEncryptor (keyBytes,keyIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Convert.ToBase64String(mStream.ToArray());
        }
        //解密：
        public static string Decrypt(string decryptString)
        {
            byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 7));
            byte[] keyIV = keyBytes;
            byte[] inputByteArray = Convert.FromBase64String(decryptString);
            var provider = new DESCryptoServiceProvider();
            var mStream = new MemoryStream();
            var cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Encoding.UTF8.GetString(mStream.ToArray());
        }
        #endregion

        //
        public static string Key = "DKMAB5DE";//加密密钥必须为8位
        //加密算法
        public static string MD5Encrypt(string pToEncrypt)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
            des.Key = ASCIIEncoding.ASCII.GetBytes(Key);
            des.IV = ASCIIEncoding.ASCII.GetBytes(Key);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }
            ret.ToString();
            return ret.ToString();
        }

        //解密算法
        public static string MD5Decrypt(string pToDecrypt)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
            for (int x = 0; x < pToDecrypt.Length / 2; x++)
            {
                int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                inputByteArray[x] = (byte)i;
            }
            des.Key = ASCIIEncoding.ASCII.GetBytes(Key);
            des.IV = ASCIIEncoding.ASCII.GetBytes(Key);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            return System.Text.Encoding.ASCII.GetString(ms.ToArray());


            //var h = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(c, "MD5");
        }


        public  static  string  MyMD5(string value)
        {
            //return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(value, "MD5");
            return value;
        }
    }
}
